package Server;

import ServerOutput.OutputManager;
import java.io.*;
import java.net.*;

public class Server implements Runnable {
    
    public static enum FileTypes {
        none, imageJpeg, imageGif, textHtml;
    }
    
    public static enum ReturnCodes {
        OK_200, BAD_REQUEST_400, FORBIDDEN_403, NOT_FOUND_404, INTERNAL_SERVER_ERROR_500, NOT_IMPLEMENTED_501;
    }
    
    public static final String SERVER_NAME = "Komandos HTTP Serveris";
    
    
    ServerSocket serverSocketObj;
    OutputManager outputManagerObj;
    Boolean acceptsConnections = false;
    
    public Server(int port, OutputManager outputMngr) throws IOException {
        serverSocketObj = new ServerSocket(port);
        outputManagerObj = outputMngr;
    }
    
    public void stop() {
        acceptsConnections = false;
    }
    
    @Override
    public void run() {
        acceptsConnections = true;
        while(acceptsConnections) {
            try {
                //Laukiam susijungimo
                serverSocketObj.setSoTimeout(2000);
                Socket connectionSocket = serverSocketObj.accept();
                //Perduodam susijungimo jungti naujam httpRequest thread'ui, ji paleidziam ir kartojam cikla
                Thread httpRequestThread = new Thread(new HttpRequest(connectionSocket, outputManagerObj));
                httpRequestThread.start();
                
            }
            catch (SocketTimeoutException ex){
            }
            
            catch (Exception ex) {
                System.out.println(ex.getMessage()); 
                continue;
            }
        }
    }
    
    public static String constructHttpHeader(ReturnCodes returnCode, FileTypes fileType) {
        //Headerio pradzia visada tokia
        String header = "HTTP/1.0 "; 
        //Endline symbolis pagal protokola
        String endln = "\r\n"; 
        
        switch (returnCode) {
          case OK_200:
            header += "200 OK";
            break;
          case BAD_REQUEST_400:
            header += "400 Bad Request";
            break;
          case FORBIDDEN_403:
            header += "403 Forbidden";
            break;
          case NOT_FOUND_404:
            header += "404 Not Found";
            break;
          case INTERNAL_SERVER_ERROR_500:
            header += "500 Internal Server Error";
            break;
          case NOT_IMPLEMENTED_501:
            header += "501 Not Implemented";
            break;
        }
        header += endln;
        header += "Connection: close" + endln;
        header += "Server: " + SERVER_NAME + endln;

        switch (fileType) {
          //plenty of types for you to fill in
          case none:
            break;
          case imageJpeg:
            header += "Content-Type: image/jpeg" + endln;
          case imageGif:
            header += "Content-Type: image/gif" + endln;
            break;
          case textHtml :
            header = header + "Content-Type: text/html" + endln;
            break;
        }
        
        header += endln;
        return header;
    }  
}